package com.qpl.depression.cms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.cms.common.ServerResponse;
import com.qpl.depression.cms.entity.LeaveMessage;
import com.qpl.depression.cms.service.LeaveMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LeaveMessageController {

    @Autowired
    private LeaveMessageService leaveMessageService;

    /**
     * 留言信息页面
     *
     * @return
     */
    @GetMapping("message/page")
    public String categoryPage() {
        return "leaveMessageList";
    }

    /**
     * 留言信息数据
     *
     * @return
     */
    @GetMapping("message/data")
    @ResponseBody
    public ServerResponse data(Long phone) {
        List<LeaveMessage> categories = leaveMessageService.list(Wrappers.<LeaveMessage>lambdaQuery().like(null != phone, LeaveMessage::getPhone, phone).eq(LeaveMessage::getStatus, 1));
        return ServerResponse.createBySuccess("success", categories);
    }

    /**
     * 留言信息删除
     *
     * @return
     */
    @GetMapping("message/del")
    @ResponseBody
    public ServerResponse messageDel(Long id) {
        return leaveMessageService.removeById(id) ? ServerResponse.createBySuccessMessage("success") : ServerResponse.createBySuccessMessage("fail");
    }

    /**
     * 留言信息编辑
     *
     * @return
     */
    @GetMapping("/leave/message/edit")
    public String leaveMessageEdit(Long id, ModelMap map) {
        LeaveMessage leaveMessage = leaveMessageService.getById(id);
        map.addAttribute("message", leaveMessage);
        return "leaveMessageEditPage";
    }

    /**
     * 留言信息保存
     *
     * @return
     */
    @PostMapping("/leave/message/save")
    @ResponseBody
    public ServerResponse leaveMessageSave(LeaveMessage leaveMessage) {
        return leaveMessageService.saveOrUpdate(leaveMessage) ? ServerResponse.createBySuccessMessage("成功") : ServerResponse.createBySuccessMessage("失败");
    }

}
